USE Lib
GO

---------------------------------------------------------------------------
-- BEGIN MAINTENANCE WRAPPER
----------------------------
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'FN' AND name = 'fn_rnd') BEGIN
	DROP  FUNCTION  tsql.fn_rnd
	PRINT '<<< DROP FUNCTION tsql.fn_rnd - Completed with SUCCESS >>>'
END

GO
----------------------------
-- END MAINTENANCE WRAPPER
---------------------------------------------------------------------------

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/*================================================================================================
Name:			tsql.fn_rnd
Type:			User defined function 
Owner:					
Description:	Generates a random number between 0 and 1 , can be used in UDF
Parameters:		@seed = seed for the randomizer max val 2147483647
Dependens on:  	none
Usage:			SELECT tsql.fnrnd(NULL)
--------------------------------------------------------------------------------------------------
Project:		http://sqllib.codeplex.com/
Help:			http://sqllib.codeplex.com/wikipage?title=fn_rnd		
Version:		200120825
SrcCtrlApp:		SVN
SrcCtrlPath:	http://sqllib.codeplex.com/SourceControl/list/changesets
Note*:			CTRL + click to follow the links above
--------------------------------------------------------------------------------------------------
History:		08/25/2012 - Adrian E Dudau - Function created
================================================================================================*/
CREATE FUNCTION tsql.fn_rnd(@seed AS INT) 
RETURNS FLOAT 
AS
BEGIN /*FUNCTION*/
	DECLARE @rnd FLOAT = 0
	DECLARE @buffer FLOAT = 0

	/*Formula  Xn+1 == ((a * Xn + c) MOD m) */  		 
	DECLARE @a  BIGINT = 0 /*multiplier: a > 0 AND a < m*/
	DECLARE @m  BIGINT = 0 /*modulus: m > 0, m > Xo >= 0*/
	DECLARE @c  BIGINT = 0 /*increment: c >= 0 AND < m */
	DECLARE @Xn BIGINT = 0  /*max value*/

	WHILE @Xn <= 10 BEGIN 
		SET @Xn = @Xn + 1; 

		IF @seed IS NULL BEGIN 
			SET @m = POWER(2.00,32) - DATEPART(MINUTE,GETDATE()) 
			SET @a = DATEPART(MS,GETDATE())
			SET @c = DATEPART(SECOND,GETDATE())
		END 
		ELSE BEGIN
			SET @m = POWER(2.0000,16)
			SET @a = 95257 
			SET @c = @seed			
		END
	
		SET @buffer = ((@a * @Xn + @c) % @m);			
		SET @rnd = (@rnd / POWER(10,LEN(@rnd))) + (@buffer / POWER(10,LEN(@buffer)))	
	END /*WHILE*/

	RETURN @rnd;  	
END /*function*/
GO 
---------------------------------------------------------------------------
-- BEGIN MAINTENANCE WRAPPER
-- Verify completion
----------------------------
IF EXISTS(SELECT * FROM sysobjects WHERE type = 'FN' AND name = 'fn_rnd') BEGIN 
	PRINT '<<< CREATE FUNCTION tsql.fn_rnd - Completed with SUCCESS >>>'
END

ELSE BEGIN 
	PRINT '<<< CREATE FUNCTION tsql.fn_rnd - Completed with ERROR >>>'
END

GO
----------------------------
-- END MAINTENANCE WRAPPER
---------------------------------------------------------------------------

--  GRANT SELECT TO PUBLIC will be used at Db level
	